<template>
  <div class="app-container" v-enter-helper="getList">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="primary" @click="exportExcel">
            <i class="el-icon-download"></i>导出
          </el-button>
          <el-button type="primary" @click="exportExcelAsync">
            <i class="el-icon-download"></i>异步导出
          </el-button>
          <el-form
            :inline="true"
            :model="listQuery"
            ref="dataList2"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-date-picker
                class="mr-3"
                v-model="dateTime"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期时间"
                end-placeholder="结束日期时间"
              >
              </el-date-picker>
              <el-select
                filterable
                v-model="listQuery.change_type"
                placeholder="选择类别"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.nickname"
                placeholder="用户昵称"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model.number="listQuery.user_code"
                placeholder="用户编号"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model.number="listQuery.mobile"
                placeholder="手机号"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item">
        <el-table
          v-loading="listLoading"
          ref="dataList"
          :data="menuList"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <!-- <el-table-column type="selection"></el-table-column> -->
          <template slot="empty">
            <span style="font-size: 14px">请选择查询条件</span>
          </template>
          <el-table-column prop="id" label="序号"> </el-table-column>
          <el-table-column prop="user_code" label="用户编号"> </el-table-column>
          <el-table-column prop="nickname" label="昵称"> </el-table-column>
          <el-table-column prop="mobile" label="会员手机"> </el-table-column>
          <el-table-column prop="user_money" label="金币数量">
          </el-table-column>
          <el-table-column prop="change_type" label="类别">
            <template slot-scope="scope">
              <div v-for="item in typeList" :key="item.value">
                <p v-if="item.value === scope.row.change_type">
                  {{ item.label }}
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="change_desc" label="备注"> </el-table-column>
          <el-table-column prop="change_time" label="时间"> </el-table-column>
        </el-table>

        <footer class="flex items-center justify-between">
          <P>总金币数量：{{ sum_coin }}</P>

          <!-- 分页 -->
          <cus-pagination
            :total="total"
            :page.sync="listQuery.page"
            :limit.sync="listQuery.perPage"
            @pagination="getList"
          />
        </footer>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  listData,
  listTypeData,
  exportExcel,
  exportExcelAsync
} from "@/api/member/accountList";
export default {
  data() {
    return {
      confirmBtn: false,

      listLoading: false,
      menuList: [],
      total: 10,
      dateTime: undefined,
      listQuery: {
        page: 1,
        perPage: 10,
        happen_time_start: undefined,
        happen_time_end: undefined,
        change_type: undefined,
        nickname: undefined,
        user_code: undefined,
        mobile: undefined
      },
      typeList: [],

      sum_coin: ""
    };
  },
  created() {
    this.getTypeList();
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;

      if (this.dateTime) {
        this.listQuery.happen_time_start = this.dateTime[0];
        this.listQuery.happen_time_end = this.dateTime[1];
      } else {
        this.listQuery.happen_time_start = undefined;
        this.listQuery.happen_time_end = undefined;
      }

      listData(this.listQuery).then(({ data: { data, total, sum_coin } }) => {
        this.menuList = data;
        this.total = total;
        this.sum_coin = sum_coin;
        this.listLoading = false;
        this.confirmBtn = false;
      });
    },
    getTypeList() {
      listTypeData().then(response => {
        response.data.forEach(item => {
          this.typeList.push({ value: item.change_type, label: item.name });
        });
      });
    },
    // 查询
    searchSubmit() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 重置
    resetSubmit() {
      this.listQuery.page = 1;
      this.listQuery.perPage = 10;
      this.listQuery.change_type = undefined;
      this.listQuery.nickname = undefined;
      this.listQuery.user_code = undefined;
      this.listQuery.mobile = undefined;

      // this.getList()
    },
    // 导出excel
    exportExcel() {
      if (this.dateTime) {
        this.listQuery.happen_time_start = this.dateTime[0];
        this.listQuery.happen_time_end = this.dateTime[1];
      } else {
        this.listQuery.happen_time_start = undefined;
        this.listQuery.happen_time_end = undefined;
      }
      let obj = {};
      for (let key in this.listQuery) {
        if (key !== "page" && key !== "perPage") {
          obj[key] = this.listQuery[key];
        }
      }

      exportExcel(obj).then(response => {
        if (response.code === 200) {
          location.href = response.data.download_url;
        }
      });
    },
    exportExcelAsync() {
      if (this.dateTime) {
        this.listQuery.happen_time_start = this.dateTime[0];
        this.listQuery.happen_time_end = this.dateTime[1];
      } else {
        this.listQuery.happen_time_start = undefined;
        this.listQuery.happen_time_end = undefined;
      }
      let obj = {};
      for (let key in this.listQuery) {
        if (key !== "page" && key !== "perPage") {
          obj[key] = this.listQuery[key];
        }
      }

      exportExcelAsync(obj).then(response => {
        if (response.code === 200) {
          this.$message.success(
            "正在导出，请稍等！可移步到 订单管理-导出日志 查看下载链接"
          );
        }
      });
    }
  }
};
</script>

<style scoped>
[v-cloak] {
  display: none;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/.el-card__header {
  padding-bottom: 0;
}
</style>
